package com.zhangwei.day31volley.utils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.zhangwei.day31volley.BaseApp;

/**
 * Volley框架封装类
 * @author zhangwei
 * @version V1.0
 * @Title: Advanced
 * @Package com.zhangwei.day31volley.utils
 * @Description:
 * @date 15/10/16 下午1:53
 *
 * 第一次 实例化Queue
 * 第二步 实例化请求
 * 第三步 添加请求
 * 第四步 取消
 */

public class VolleyHelper {
    private static VolleyHelper helper;
    private RequestQueue queue;


    private ImageLoader imageLoader;

    private VolleyHelper() {

    }

    public static VolleyHelper getInstance() {
        if (helper == null) {
            helper = new VolleyHelper();
        }
        return helper;
    }

    public RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(BaseApp.getInstance());
        }
        return queue;
    }

    public void addRequest(Request<?> request, Object tag) {
        if (getQueue() != null) {
            request.setShouldCache(true);
            request.setTag(tag);
            getQueue().add(request);
        }
    }

    public void cancelAll(Object tag) {
        getQueue().cancelAll(tag);
    }

    /**
     * ImageLoader
     */
    public ImageLoader getImageLoader() {
        if (imageLoader == null) {
            imageLoader = new ImageLoader(getQueue(), AppImageCache.getInstance());
        }
        return imageLoader;
    }
}
